package com.yqy.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Map;
import java.util.Objects;

@Controller
public class ScopeTestController {
    @RequestMapping("/test/servletAPI")
    public String testServletAPI(HttpServletRequest request){
        request.setAttribute("testRequestScope","Hello！ServletAPI");
        return "success";
    }

    @RequestMapping("/test/mov")
    public ModelAndView testModelAndView(){
        ModelAndView mov=new ModelAndView();
        mov.setViewName("success");
        mov.addObject("testRequestScope","Hello！ModelAndView");
        return mov;
    }

    @RequestMapping("/test/model")
    public String testModel(Model model){
        model.addAttribute("testRequestScope","Hello！Model");
        System.out.println(model.getClass().getName());
        return "success";
    }

    @RequestMapping("/test/map")
    public String testMap(Map<String, Object> map){
        map.put("testRequestScope","Hello！Map");
        map.getClass().getName();
        return "success";
    }

    @RequestMapping("/test/modelMap")
    public String testModelMap(ModelMap modelMap){
        modelMap.addAttribute("testRequestScope","Hello！ModelMap");
        System.out.println(modelMap.getClass().getName());
        return "success";
    }

    @RequestMapping("/test/session")
    public String testSession(HttpSession session){
        session.setAttribute("testSessionScope","Hello！Session");
        return "success";
    }

    @RequestMapping("/test/application")
    public String testApplication(HttpSession session){
        ServletContext context = session.getServletContext();
        context.setAttribute("testApplicationScope","Hello！Application");
        return "success";
    }
}
